<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html,
        body {
            width: 100%;
            height: 100%;
        }

        * {
            margin: 0;
            padding: 0;
        }

        #mask {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: #000;
            opacity: 0.5;
            filter: alpha(opacity=50);
            display: none;
        }

        #box {
            position: absolute;
            top: 50%;
            left: 50%;
            width: 300px;
            height: 200px;
            z-index: 99;
            background: blue;
            transform: translate(-50%,-50%);
            display: none;
        }

        #cancel {
            color: #fff;
            position: absolute;
            right: 20px;
            top: 0;
            background: #000;
            padding: 0 10px;
        }
        p{
            width: 100%;
            height: 20px;
            background: cadetblue;
        }
    </style>
</head>

<body>
    <div id="mask">
    </div>
    <div id="box">
        <p><span id="cancel">x</span></p>
    </div>
    <input type="button" id="btn" value="弹出层">

</body>
<script>
 window.onload = function(){
    var  mask = document.getElementById('mask')
    var box = document.getElementById('box')
    var btn = document.getElementById('btn')
    var cancel = document.getElementById('cancel')
    btn.onclick = function(){
        mask.style.display = "block"
        box.style.display = "block"
    }
    cancel.onclick = function(){
        mask.style.display = "none"
        box.style.display = "none"
    }
 }
</script>
</html>